const express = require('express');
const connection = require('../mysql') // 获取连接实例
const app = express();
const $sql = require('../db');
var router = express.Router();
const { query } = require('../db');
//通过json这个中间件解析json格式的数据
app.use(express.json())
//通过使用urlencoded解码数据
app.use(express.urlencoded({ extended: false }))


// 充值
app.post('/chongzhi', async function (req, res, next) {
    // 获取参数
    var id = Number(req.body.id);
	var account=req.body.account;
	var money=Number(req.body.money);

    var sql = `update  user set balance=balance+${money} where id=${id} and  account='${account}' `;
	console.log(sql)
    // 查询数据库
    var result = await query(sql).catch(next)
    // 返回数据
    if (result) {
        res.json({
            code: 200,
            msg: "充值成功!"
        })
    } else {
        res.json({
            code: -9999,
            msg: "系统错误"
        })
    }
})



// 获取用户信息列表
app.post('/list', async function (req, res,next) {
	// 获取分页参数
	console.log(req.body,"--")
	var key = req.body.key || '';
	var user_type=req.body.user_type;
	var pageNum = Number(req.body.pageNum);
	var pageSize = Number(req.body.pageSize);
	var start = (pageNum - 1) * pageSize;
	var sql="";
	if(user_type==1){
		sql = `select * from user where user_type=0 and supername like '%${key}%'   ORDER BY id desc   limit ${start},${pageSize} `;
	}
	else if(user_type==2){
		sql = `select * from user where user_type=1 and supername like '%${key}%'   ORDER BY id desc   limit ${start},${pageSize} `;

	}
	// 查询数据库
	var result = await query(sql).catch(next)
	var sql_all = 'select * from  user';
	var result_all = await query(sql_all).catch(next);
	// 返回数据
	if (result) {
		res.json({
			code: 200,
			data: result,
			total: result_all.length
		})
	} else {
		res.json({
			code: -9999,
			msg: "系统错误"
		})
	}
})

// 注册
app.post('/reg', async function (req, res,next) {
	// 获取分页参数
	console.log(req.body,"--")
	
	var user_type=req.body.user_type;
	var supername=req.body.supername;
	var account=req.body.account;
	var password=req.body.password;

	var sex="";
	var email="";
	var address="";
	var headimg="https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png";
	var check_sql=`select * from user where  account='${account}'`;
	var check_res=await query(check_sql).catch(next);
	if(check_res.length>=1){
		res.json({
			code: -9999,
			msg: "注册账号已经存在!"
		})
		return false
	}


	var sql=`insert into user(user_type,supername,account,password,headimg) values('${user_type}','${supername}','${account}','${password}','${headimg}' )`;
	
	// 查询数据库
	var result = await query(sql).catch(next)
	
	// 返回数据
	if (result) {
		res.json({
			code: 200,
			msg:"注册成功！"
		})
	} else {
		res.json({
			code: -9999,
			msg: "系统错误"
		})
	}
})


// 删除
app.post('/del', async function (req, res, next) {
    // 获取参数
    var id = Number(req.body.id);
    var sql = `delete from user where id=${id} `
    // 查询数据库
    var result = await query(sql).catch(next)
    // 返回数据
    if (result) {
        res.json({
            code: 200,
            msg: "删除成功!"
        })
    } else {
        res.json({
            code: -9999,
            msg: "系统错误"
        })
    }
})

//获取个人信息
app.post('/pergetdata', (req, res) => {
	var params = req.body.account;
	var sql = $sql.super_sql.get_superdata;
	console.log(params)
	connection.query(sql, params, (err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})

//更新个人信息
app.put('/perupdata', (req, res) => {
	var params = [req.body.paymentPassword,req.body.supername, req.body.email, req.body.tel, req.body.address, req.body.headimg, req.body.account]
	var sql = $sql.super_sql.per_superupdata;
	connection.query(sql, params, (err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})




module.exports = app;